<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>银行信息设置</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<%@include file="common.jsp"%>
</head>
<body style="margin-top:-10px;">
	<div style="margin-top:20px;">
		<button onclick="loadBank()" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#addBankBox">新增</button>
		<button onclick="updateBankBox()" class="btn btn-primary btn-lg">修改</button>
		<button onclick="delBank()" class="btn btn-primary btn-lg">删除</button>
	</div>
	<div>
		<table id="bankTable" class="text-nowrap"></table>
	</div>
	<div class="modal fade" id="addBankBox" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">添加银行信息</h4>
                <h4 class="modal-title" id="myModalLabel2">修改银行信息</h4>
            </div>
            <div class="modal-body">
           		 <form id="bankForm" method='post' class="" enctype="multipart/form-data">
				 	<div class="form-group">
				    	<label for="exampleInputName2">银行编码：</label>
				    	<input id="bankCode" name="bankCode" class="form-control"/>
				    	<input id="id" name="id" hidden="ture"/>
				  	</div>
				  	<div class="form-group">
				    	<label for="exampleInputEmail2">银行名称：</label>
				    	<input id="bankName" name="bankName" class="form-control"/>
				  	</div>
				  	<div class="form-group">
				  		<label for="exampleInputEmail2">兑换信息：</label>
				  		<input id="bankExchange" name="bankExchange" class="form-control"/>
				  	</div>
				  	<div class="form-group">
				  		<label for="exampleInputEmail2">积分(万)/金额：</label>
				  		<input id="inceptionUnit" name="inceptionUnit" type="number" class="form-control"/>
				  	</div>
				  	<div class="form-group">
				  		<label for="exampleInputEmail2" style="float:left;">银行信息：</label>
				  		<input id="bankImg" name="img" type="file" />
				  	</div>
				  	<div class="form-group">
				  		<label for="exampleInputEmail2" style="float:left;">银行图标：</label>
				  		<input id="bankImg" name="logoImg" type="file" />
				  	</div>
				</form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="addBank" onclick="submitBank()">提交</button>
                <button type="button" class="btn btn-primary" id="updateBank" onclick="updateBank()">修改</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
	</div>
</body>
<script type="text/javascript">
//重载表格
$(function(){
	//加载表格
	$("#bankTable").bootstrapTable({
		url: "${pageContext.request.contextPath}/bank/findBank.do",
		method: "post",
		cache: false,
		dataType: "json",
		contentType: "application/x-www-form-urlencoded",
		toolbar: "#toolbar",
		classes: "table table-hover table-striped",
		height:450,
		pagination: true,
		sidePagination: 'server',
		search: false,
		smartDisplay:false,
		sortOrder: "desc",
		searchText: "",
		showRefresh: false,
		showToggle: false,
		singleSelect: true,
		showPaginationSwitch: false,
		pageNumber: 1,
		pageSize: 5,
		pageList: [5, 10, 20, 40],
		clickToSelect: true,
		onCheck: function (row) {
		},
		queryParamsType: "undefined",
		queryParams: function (params) {
			var params = {
				pageNum: params.pageNumber,
				pageSize: params.pageSize,
				sortBy: params.sortOrder,
			}
			return params;
		},
		columns: [
			{ checkbox: true }
			,  {
				field: "bankCode",
				title: "银行编码"
			}, {
				field: "bankName",
				title: "用户名称"
			}, {
				field: "bankExchange",
				title: "积分兑换信息",
			}, {
				field: "inceptionUnit",
				title: "积分(万)/金额"
			}, {
				field: "deleteFlag",
				title: "删除标识",
				formatter: formatDeleteFlag
			}, {
				field: "createTime",
				title: "创建时间",
				formatter: formatTime
			}
			
		],
		responseHandler: function (data) {//onLoad前处理数据源
			return data;
		},
		onLoadSuccess: function (data) {	//成功时执行
		},
		onLoadError: function (data) {	//失败时执行
			return;
		}
	});
	//重载表格
	$(window).resize(function () {
		$('#bankTable').bootstrapTable('resetView');
	});
})
//打开修改银行信息-点击事件
function updateBankBox(){
	var rows = $('#bankTable').bootstrapTable('getSelections');
	if(rows.length <= 0){
		return alert("请选择数据！");
	}
	//打开修改框
	$('#addBankBox').modal('show');
	//模块框标题的文字显示与隐藏
	$('#myModalLabel').css("display","none");
	$('#myModalLabel2').css("display","block");
	//操作按钮隐藏与显示
	$('#updateBank').show();
	$("#addBank").hide();
	
	var row = rows[0];
	//控件赋值
	//银行编码
	$('#bankCode').val(row.bankCode);
	//银行名称
	$('#bankName').val(row.bankName);
	//兑换信息
	$('#bankExchange').val(row.bankExchange);
	//积分(万)/金额
	$('#inceptionUnit').val(row.inceptionUnit);
	//ID
	$('#id').val(row.id);
	
	//控件置灰
	$('#bankCode').attr("disabled","disabled");
}
//打开新增银行信息-点击事件
function loadBank(){
	//模块框标题的文字显示与隐藏
	$('#myModalLabel').css("display","block");
	$('#myModalLabel2').css("display","none");
	//操作按钮隐藏与显示
	$('#addBank').show();
	$("#updateBank").hide();
	//清空表单
	document.getElementById("bankForm").reset()
	//取消置灰
	$('#bankCode').removeAttr("disabled");
}
//新增银行信息
function submitBank(){
	if($('#bankCode').val() == ''){
		return alert("请输入银行编码！");
	}
	if($('#bankName').val() == ''){
		return alert("请输入银行名称！");
	}
	if($('#bankExchange').val() == ''){
		return alert("请输入银行积分兑换信息！");
	}
	if($('#inceptionUnit').val() == ''){
		return alert("请输入金额！");
	}
	//只传递银行编码做为查询条件
	var datas = "bankCode="+$('#bankCode').val();
	//银行编码数据唯一性校验
	$.ajax({
		url:"${pageContext.request.contextPath}/bank/findBank.do",
		contentType:"application/x-www-form-urlencoded",
		//只把银行编码传递到后台
		data: datas,
		type:"post",
		processData: false,
		cache:false,//false是不缓存，true为缓存
	    async:true,//true为异步，false为同步
		success:function(data){
			var datas = jQuery.parseJSON(data);
			if(datas.rows.length > 0){
				return alert('编码存在！');
			}else{
				//提交表单，新增银行信息
				$('#bankForm').attr("action","${pageContext.request.contextPath}/bank/addBank.do");
				$('#bankForm').submit();
				alert('新增成功！');
			}
		}
	})
}
//修改银行信息
function updateBank(){
	if(confirm("确认修改？")){
		//from对象序列化
		//var data = $('#bankForm').serialize();
		//var submitData = decodeURIComponent(data,true);
		var submitData = new FormData(document.getElementById("bankForm"));
		$.ajax({
			url: "${pageContext.request.contextPath}/bank/updateBank.do",
			data: submitData,
			type: "post",
			processData: false,
			contentType:false,
			success:function(data){
				alert(data);
				//刷新表格数据
				$('#bankTable').bootstrapTable('refresh');
				//隐藏模态框
				$('#addBankBox').modal('hide');
			}
		})
	}
}
//删除银行信息
function delBank(){
	var rows = $('#bankTable').bootstrapTable('getSelections');
	if(rows.length <= 0){
		return alert("请选择数据！");
	}
	if(confirm("确认删除？")){
		//from对象序列化
		//var data = $('#bankForm').serialize();
		//var submitData = decodeURIComponent(data,true);
		var row = rows[0];
		$.ajax({
			url:"${pageContext.request.contextPath}/bank/delBank.do",
			data: {
				id:row.id
			},
			type:"post",
			dataType:"json",
			processData: true,
			success:function(data){
				alert(data);
				//刷新表格数据
				$('#bankTable').bootstrapTable('refresh');
			}
		})
	}
}
</script>
</html>




